package com.mzj.saas.mservice.smartlock.service.tianwanghulian;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mzj.saas.commons.HttpUtils;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.lock.Repository.SmartLockAuthRepository;
import com.mzj.saas.mservice.lock.entity.SmartLockAuth;
import com.mzj.saas.mservice.lock.entity.SmartLockBle;
import com.mzj.saas.mservice.smartlock.service.yaluete.SmartLockYltService;
import com.mzj.saas.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 天网互联-蓝牙授权
 *
 * @author shun
 * @createDate: 2021年6月26日 下午3:10:56
 * @version: 1.0
 *
 */
@Service
public class BluetoothService {
    private static final Logger log = LoggerFactory.getLogger(SmartLockYltService.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private SmartLockAuthRepository smartLockAuthRepository;

    public ResultBean<String> empower(String sn,Integer type,SmartLockBle smartLockBle){
        log.info("天网互联 sn={}", sn);
        //SmartLockPwd identification = smartLockPwdRepository.identification(pwd.getLockId());
        SmartLockAuth smartLockAuthById = smartLockAuthRepository.findSmartLockAuthById(smartLockBle.getId());
        String token = redisService.getToken();
        Map<String, Object> hashMap = new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("deviceNum",sn);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String get=null;
        long l = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        String sDate = sdf.format(date);  //开始时间
        String s = HttpUtils.httpClientGet("https://paas.locksuiyi.com/deviceManagement/api/bluetooth/unlockAuthority.do", token, maps);
        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(JSON.parseObject(s).get("data")));
        Optional<SmartLockAuth> authOpt = smartLockAuthRepository.findById(smartLockBle.getLockAuthId());
        SmartLockAuth smartLockAuth = authOpt.get();
        Snowflake snowflake = new Snowflake(1L,1L);
        String uuid = snowflake.nextIdStr();
        smartLockAuth.setIdentification(uuid);
        smartLockAuthRepository.setIdentification(uuid,smartLockAuth.getId());
        if(type==1){
            cal.setTime(date);
            cal.add(Calendar.YEAR, 10);// 年
            date = cal.getTime();
            String formats = sdf.format(date); //结束时间
            List<Object> list = new ArrayList<>();
            map.put("identification",uuid);
            map.put("startTime", sDate);
            map.put("endTime", formats);
            list.add(map);
            hashMap.put("deviceNum",sn);
            hashMap.put("passwordList", list);
            get= HttpUtils.httpClientPost("https://paas.locksuiyi.com/deviceManagement//api/bluetooth/unlockAuthority.do", JSON.toJSONString(hashMap), token);
        }
        if(type==2){
            cal.setTime(date);
            cal.add(Calendar.HOUR, 24);// 24小时制
            date = cal.getTime();
            String formats = sdf.format(date); //结束时间
            List<Object> list = new ArrayList<>();
            map.put("identification",uuid);
            map.put("startTime", sDate);
            map.put("endTime", formats);
            list.add(map);
            hashMap.put("deviceNum",sn);
            hashMap.put("passwordList", list);
            get= HttpUtils.httpClientPost("https://paas.locksuiyi.com/deviceManagement//api/bluetooth/unlockAuthority.do", JSON.toJSONString(hashMap), token);
        }
        if(type==3){
            cal.setTime(date);
            cal.add(Calendar.YEAR, 1);// 24小时制
            date = cal.getTime();
            String formats = sdf.format(date); //结束时间
            List<Object> list = new ArrayList<>();
            map.put("identification",uuid);
            map.put("startTime", sDate);
            map.put("endTime", formats);
            list.add(map);
            hashMap.put("deviceNum",sn);
            hashMap.put("passwordList", list);
            get= HttpUtils.httpClientPost("https://paas.locksuiyi.com/deviceManagement//api/bluetooth/unlockAuthority.do", JSON.toJSONString(hashMap), token);
        }
        return ResultBean.successfulResult(JSON.toJSONString(get));
    }
}
